package com.vviton.zyhj.common.utils.token;

import java.util.Date;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

import redis.clients.jedis.Jedis;

import com.vviton.zyhj.common.common.HCacheConstants;
import com.vviton.zyhj.common.utils.ResultJson;
import com.wdcloud.hcache.redis.connection.DataSourceManager;
import com.wdcloud.hcache.redis.connection.IRedisTemplate;
import com.wdcloud.hcache.redis.connection.RedisCallback;

/**
 * ClassName: TokenManager <br/>
 * Function: Token管理. <br/>
 * Date: 2016年4月13日 <br/>
 *
 * @author weiming.chen
 * @version 1.0
 * @since JDK 1.7
 */
public abstract class TokenManager {

    /** 随机数 **/
    private static Random random = new Random();

    /** 过期时间 */
    private static final int EXPIRED_TIME = 180;

    /**
     * GenerateToken,(生成token). <br/>
     * Author: qiaocf <br/>
     * Create Date: 2016年4月12日 <br/>
     * ===============================================================<br/>
     * Modifier: qiaocf <br/>
     * Modify Date: 2016年4月12日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param key key值
     * @param zh 账号
     * @return String
     * @since JDK 1.7
     */
    public static String generateToken(final String key, final String zh) {
        IRedisTemplate redisTemplate = DataSourceManager.newInstance().getRedisTemplate(HCacheConstants.CH_REDIS_DS_NAME);
        final String mykey = getKey(key);
        String pageToken = (String) redisTemplate.execute(false, new RedisCallback<Object>() {
            @Override
            public Object execute(Jedis jedis) {
                String redisKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "pageToken";
                // 将对象写入redis
                return jedis.get(redisKey);
            }
        });
        String token = (String) redisTemplate.execute(false, new RedisCallback<Object>() {
            @Override
            public Object execute(Jedis jedis) {
                String redisKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "token";
                // 将对象写入redis
                return jedis.get(redisKey);
            }
        });
        if (StringUtils.isBlank(pageToken) && StringUtils.isBlank(token)) {
            final String curToken = (int) random.nextLong() + String.valueOf(new Date().getTime());
            redisTemplate.execute(false, new RedisCallback<Object>() {
                @Override
                public Object execute(Jedis jedis) {
                    String redisPageKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "pageToken";
                    String redisKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "token";
                    jedis.setex(redisPageKey, EXPIRED_TIME, curToken);
                    jedis.setex(redisKey, EXPIRED_TIME, curToken);
                    return "";
                }
            });
        }
        return "";
    }

    /**
     *
     * setToken,(设置token). <br/>
     * Author: qiaocf <br/>
     * Create Date: 2016年4月12日 <br/>
     * ===============================================================<br/>
     * Modifier: qiaocf <br/>
     * Modify Date: 2016年4月12日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param key key值
     * @param zh 账号
     * @return ResultJson
     * @since JDK 1.7
     */
    public static ResultJson setToken(final String key, final String zh) {
        ResultJson rJson = new ResultJson(false);
        IRedisTemplate redisTemplate = DataSourceManager.newInstance().getRedisTemplate(HCacheConstants.CH_REDIS_DS_NAME);
        final String mykey = getKey(key);
        String pageToken = (String) redisTemplate.execute(false, new RedisCallback<Object>() {
            @Override
            public Object execute(Jedis jedis) {
                String redisKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "pageToken";
                // 将对象写入redis
                return jedis.get(redisKey);
            }
        });
        String token = (String) redisTemplate.execute(false, new RedisCallback<Object>() {
            @Override
            public Object execute(Jedis jedis) {
                String redisKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "token";
                // 将对象写入redis
                return jedis.get(redisKey);
            }
        });
        if (pageToken == null && token == null) {
            return rJson;
        }
        if (pageToken != null && pageToken.equals(token)) {
            rJson.setResult(true);
            final String curToken = (int) random.nextLong() + String.valueOf(new Date().getTime());
            redisTemplate.execute(false, new RedisCallback<Object>() {
                @Override
                public Object execute(Jedis jedis) {
                    String redisKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "token";
                    jedis.setex(redisKey, EXPIRED_TIME, curToken);
                    return "";
                }
            });
        }
        return rJson;
    }

    /**
     *
     * deleteToken,(删除token). <br/>
     * Author: qiaocf <br/>
     * Create Date: 2016年4月12日 <br/>
     * ===============================================================<br/>
     * Modifier: qiaocf <br/>
     * Modify Date: 2016年4月12日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param key key值
     * @param zh 账号
     * @since JDK 1.7
     */
    public static void deleteToken(final String key, final String zh) {
        IRedisTemplate redisTemplate = DataSourceManager.newInstance().getRedisTemplate(HCacheConstants.CH_REDIS_DS_NAME);
        final String mykey = getKey(key);
        redisTemplate.execute(false, new RedisCallback<Object>() {
            @Override
            public Object execute(Jedis jedis) {
                String redisPageKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "pageToken";
                String redisKey = mykey + ":" + HCacheConstants.RESUBMIT_TOKEN + ":" + key + "_" + zh + "token";
                jedis.del(redisPageKey);
                jedis.del(redisKey);
                return "";
            }
        });
    }

    /**
     *
     * getKey,(根据页面的key来确认存放redis的key的开头). <br/>
     * Author: qiaocf <br/>
     * Create Date: 2016年4月13日 <br/>
     * ===============================================================<br/>
     * Modifier: qiaocf <br/>
     * Modify Date: 2016年4月13日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param key key值
     * @return String
     * @since JDK 1.7
     */
    private static String getKey(String key) {
        String myKey = "";
        String[] keyArr = key.split("_");
        if ("pt".equals(keyArr[1])) {
            myKey = HCacheConstants.YYXT_REDIS_AM;
        }
        if ("xfz".equals(keyArr[1])) {
            myKey = HCacheConstants.YYXT_REDIS_CS;
        }
        if ("dsf".equals(keyArr[1])) {
            myKey = HCacheConstants.YYXT_REDIS_EP;
        }
        return myKey;
    }

}
